package com.o2opuzi.ms.service.user;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.o2opuzi.ms.dao.user.UserDao;
import com.o2opuzi.ms.dao.user.UserLoginLogDao;
import com.o2opuzi.ms.model.user.User;
import com.o2opuzi.ms.model.user.UserLoginLog;
import com.o2opuzi.ms.service.user.exception.UserLoginException;

@Service
public class UserLoginService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserLoginLogDao userLoginLogDao;
	
	
	/**
	 * 用户登录
	 * @auther Y.hj
	 * @param account
	 * @param password
	 * @param ip
	 * @return
	 * @throws LoginException 
	 */
	@Transactional
	public User login(String account,String password,String ip,String client) throws UserLoginException{
		if(StringUtils.isEmpty(account) || StringUtils.isEmpty(password)){
			throw new UserLoginException("账号或密码不能为空");
		}
		User user = userDao.findUser(account, password);
		if(user == null){
			throw new UserLoginException("账号或密码错误");
		}
		//添加登录日志
		UserLoginLog userLoginLog = new UserLoginLog();
		userLoginLog.setUser_id(user.getId());
		userLoginLog.setLogin_ip(ip);
		userLoginLog.setLogin_client(client);
		userLoginLogDao.addUserLoginLog(userLoginLog);		
		return user;
		
	}
	
}
